package com.seuic.hid_demo.base

import android.os.Bundle
import android.os.Process
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.PermissionUtils

abstract class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getContentLayout())
        BusUtils.register(this)
        PermissionUtils.permission(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    initView()
                    initData()
                    initAction()
                }

                override fun onDenied() {}
            }).request()
    }

    protected abstract fun getContentLayout(): Int

    protected abstract fun initView()

    protected abstract fun initData()

    protected abstract fun initAction()

    override fun onStop() {
        super.onStop()
        if (isFinishing||isDestroyed){
            BusUtils.unregister(this)
        }
    }

    /**
     * 判断事件出发时间间隔是否超过预定值
     *
     * @return
     * @Description
     */
    private var firstExitClickTime //用于检测双击退出
            : Long = 0

    open fun isDoubleClickExit(): Boolean {
        val curtime = System.currentTimeMillis()
        val timeD = curtime - firstExitClickTime
        if (timeD in 1..1999) {
            return true
        }
        firstExitClickTime = curtime
        return false
    }

    open fun appExit() {
        try {
            Process.killProcess(Process.myPid())
            System.exit(0)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}